package com.project.web.controller.user;

import java.util.List;

import com.project.user.domain.UserOrder;
import com.project.user.domain.UserOrderRemark;
import com.project.user.service.IUserOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.project.user.domain.UserGoodsRemark;
import com.project.user.service.IUserGoodsRemarkService;
import com.project.web.controller.common.BaseController;
import com.project.common.base.TableDataInfo;
import com.project.common.base.AjaxResult;

/**
 * 餐饮评价 信息操作处理
 *
 * @author administrator
 */
@Controller
@RequestMapping("/user/remark")
public class UserGoodsRemarkController extends BaseController {
    private String prefix = "normal";

    @Autowired
    private IUserOrderService userOrderService;
    @Autowired
    private IUserGoodsRemarkService userGoodsRemarkService;

    /**
     * 跳转到菜品评论页面
     *
     * @param orderId
     * @param mmap
     * @return
     */
    @GetMapping("/{orderId}")
    public String userGoodsRemark(@PathVariable("orderId") Long orderId, ModelMap mmap) {
        UserOrder order = userOrderService.selectUserOrderById(orderId);
        mmap.put("order", order);
        mmap.put("menuType", "order");
        mmap.put("topTitle", "用户中心");
        return prefix + "/user_remark_save";
    }

    /**
     * 查询餐饮评价列表
     *
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list() {
        Long userId = this.getUserId();
        startPage();
        List<UserOrderRemark> list = userGoodsRemarkService.selectOrderRemarkForUser(userId);
        return getDataTable(list);
    }

    /**
     * 新增保存餐饮评价
     *
     * @param goodsId
     * @param msg
     * @param score
     * @param orderId
     * @return
     */
    @PostMapping("/save")
    public String addSave(Long[] goodsId, String[] msg, Integer[] score, Long orderId) {
        UserGoodsRemark[] remarks = new UserGoodsRemark[goodsId.length];
        for (int i = 0; i < goodsId.length; i++) {
            remarks[i] = new UserGoodsRemark(orderId, goodsId[i], this.getUserId(), score[i], msg[i]);
        }
        userGoodsRemarkService.insertUserGoodsRemark(remarks);
        return "redirect:/user/remark";
    }

    /**
     * 删除餐饮评价
     *
     * @param id
     * @return
     */
    @GetMapping("/remove")
    public String remove(Long id) {
        userGoodsRemarkService.deleteUserGoodsRemarkByIds(id.toString());
        return "redirect:/user/remark";
    }

    /**
     * 删除餐饮评价(多选)
     *
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    public AjaxResult remove(String ids) {
        return toAjax(userGoodsRemarkService.deleteUserGoodsRemarkByIds(ids));
    }

    /**
     * 跳转用户菜品评价页面
     *
     * @return
     */
    @RequiresPermissions("system:userGoodsRemark:view")
    @GetMapping("/seller")
    public String remark() {
        return "/system/userGoodsRemark/userGoodsRemark";
    }

    /**
     * 查询店家的菜品评价
     *
     * @param userGoodsRemark
     * @return
     */
    @PostMapping("/listForSeller")
    @ResponseBody
    public TableDataInfo listForSeller(UserGoodsRemark userGoodsRemark) {
        if (!this.getUser().isAdmin()) {
            userGoodsRemark.getParams().put("sellerId", this.getUserId());
        }
        startPage();
        List<UserGoodsRemark> list = userGoodsRemarkService.selectUserGoodsRemarkList(userGoodsRemark);
        return getDataTable(list);
    }
}
